/*
 * Copyright © 2012 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners.
 * See LICENSE.TXT for license information.
 */

package com.nokia.example.drumkit.components;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class Drum {
    private Sprite sprite;
    private boolean repaint;
    private boolean hit;
    private int index;
    private int x;
    private int y;

    /**
     * Constructor
     * @param image drum bitmap
     * @param index SoundManager index of the drum
     * @param x x coordinate of the drum image
     * @param y y coordinate of the drum image
     * @param frames number of animation frames
     */
    public Drum(Image image, int index, int x, int y, int frames) {
        sprite = new Sprite(image, image.getWidth(), image.getHeight() / frames);
        sprite.setPosition(x, y);
        this.index = index;
        this.x = x;
        this.y = y;
        repaint = false;
        hit = false;
    }

    public boolean needsRepaint() {
        return repaint;
    }

    public void repaint(boolean repaint) {
        this.repaint = repaint;
    }

    public boolean isHit() {
        return hit;
    }

    public void setHit(boolean hit) {
        this.hit = hit;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getWidth() {
        return sprite.getWidth();
    }

    public int getHeight() {
        return sprite.getHeight();
    }

    public void hit() {
        hit = true;
        repaint = true;
    }

    public void release() {
        repaint = true;
    }

    public boolean isHit(int x, int y) {
        return ((x > this.x)
                && (y > this.y)
                && (x < (this.x + sprite.getWidth())
                && (y < (this.y + sprite.getHeight()))));
    }

    public void draw(Graphics g) {
        if (hit) {
            sprite.paint(g);
            sprite.nextFrame();

            hit = false;
        }
        else {
            if (sprite.getFrame() != 0) {
                sprite.paint(g);
                sprite.nextFrame();
            }
            else {
                repaint = false;
            }
        }
    }
}
